import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class LeaderBoardMenu extends ScreenManager{

	private static final long serialVersionUID = 1L;
	private String userName = "";
	private ArrayList<Integer> globalScores = new ArrayList<Integer>();
	private ArrayList<Integer> personalScores = new ArrayList<Integer>();
	private ArrayList<String> globalNames = new ArrayList<String>();
	private int levelNo;
	private JLabel globalLabel, personalLabel;
	
	public LeaderBoardMenu(String name){
		userName = name;
		final JPanel leaderBoardMenu = new JPanel();
		getContentPane().add(leaderBoardMenu);
		
		final JComboBox<String> levelComboBox = new JComboBox<String>(new String[] { "Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", "Level 7", "Level 8", "Level 9", "Level 10", "Level 11", "Level 12", "Level 13", "Level 14", "Level 15" });
		levelComboBox.setBounds(150, 45, 100, 30);
		leaderBoardMenu.add(levelComboBox);
		
		ImageIcon uniBg = new ImageIcon(getClass().getResource("bgs/universalBg.jpg"));
		ImageIcon persBg = new ImageIcon(getClass().getResource("bgs/personalBg.jpg"));
		ImageIcon backBg = new ImageIcon(getClass().getResource("bgs/backBg.jpg"));
		

		globalLabel = new JLabel("");
    	globalLabel.setBounds(65, 70, 160, 175);
    	globalLabel.setOpaque(true);
    	personalLabel = new JLabel("");
    	personalLabel.setBounds(225, 70, 160, 175);
    	personalLabel.setOpaque(true);

		
		JButton universalScoresButton = new JButton("Universal Scores", uniBg);
		universalScoresButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
				levelNo = levelComboBox.getSelectedIndex() + 1;
		    	fillGlobalScores();
		    	globalLabel.setText(fillGlobalLabel(globalScores, globalNames));
				leaderBoardMenu.add(globalLabel);
				repaint();
		    }
		});
		
		JButton personalScoresButton = new JButton("Personal Scores", persBg);
		personalScoresButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
				levelNo = levelComboBox.getSelectedIndex() + 1;
		    	fillPersonalScores(userName);
		    	personalLabel.setText(fillPersonalLabel(personalScores));
		    	leaderBoardMenu.add(personalLabel);
		    	repaint();
		    }
		});
		
		JButton backButton = new JButton("Back", backBg);
		backButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	MainMenu mainMenu = new MainMenu(userName);
		    	mainMenu.setVisible(true);
		    }
		});
		
		leaderBoardMenu.setLayout(null);
		universalScoresButton.setBounds(65, 250, 98, 55);
		personalScoresButton.setBounds(225, 250, 98, 55);
		backButton.setBounds(150,315,95,35);
		
		leaderBoardMenu.add(universalScoresButton);
		leaderBoardMenu.add(personalScoresButton);
		leaderBoardMenu.add(backButton);
		
		WindowListener exitListener = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
		    	MainMenu mainMenu = new MainMenu(userName);
		    	mainMenu.setVisible(true);
            }
        };
        this.addWindowListener(exitListener);
	}
	
	private void fillGlobalScores(){
		DatabaseManager.openConnection();
		Statement stmt = null;
		fillZeros();
		try {
			String sql = "Select highscores.score, users.userName, highscores.levelId from flappybirdv2db.highscores inner join flappybirdv2db.users on highscores.userID = users.userID inner join flappybirdv2db.levels on levels.id = highscores.levelId where levelId = " + levelNo + " order by score desc limit 10";
			stmt = DatabaseManager.conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			int i = 0;
			while(rs.next()){
				globalScores.set(i,rs.getInt("score"));
				globalNames.set(i, rs.getString("userName"));
				i++;
			}
			DatabaseManager.closeConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void fillPersonalScores(String name){
		DatabaseManager.openConnection();
		Statement stmt = null;
		fillZeros();
		try {
			String sql = "Select highscores.score, users.userName, highscores.levelId from flappybirdv2db.highscores inner join flappybirdv2db.users on highscores.userID = users.userID inner join flappybirdv2db.levels on levels.id = highscores.levelId where levelId = " + levelNo + " and userName = '"+ userName+"' order by score desc limit 10";
			stmt = DatabaseManager.conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			int i = 0;
			while(rs.next()){
				personalScores.set(i,rs.getInt("score"));
				i++;
			}
			DatabaseManager.closeConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String fillPersonalLabel(ArrayList<Integer> scoreList){
		String labelString = "<html>";
		for(int i = 0; i < scoreList.size(); i++){
			labelString = labelString + "<br>" + (i+1) + ": " + scoreList.get(i) + "</br>";
		}
		labelString = labelString + "</html>";
		return labelString;
	}
	private String fillGlobalLabel(ArrayList<Integer> scoreList, ArrayList<String> personNames){
		String labelString = "<html>";
		for(int i = 0; i < scoreList.size(); i++){
			labelString = labelString + "<br>" + (i+1) + ": " + scoreList.get(i) + ", " + personNames.get(i) + "</br>";
		}
		labelString = labelString + "</html>";
		return labelString;
	}
	private void closeTab(){
    	this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING));
	}
	private void fillZeros(){
		if(globalScores.size() ==0){
			for(int i = 0; i < 10; i++){
				globalScores.add(0);
				personalScores.add(0);
				globalNames.add("");
			}
		}
		else{
			for(int i = 0; i < 10; i++){
				globalScores.set(i, 0);
				personalScores.set(i, 0);
				globalNames.set(i, "");
			}
		}
	}
}
